package com.boat.rosbridge;

import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.example.sparkchaindemo.app.ApplicationCallback;
import com.example.sparkchaindemo.app.SparkHelper;
import com.example.sparkchaindemo.service.SparkService;
import com.example.sparkchaindemo.utils.SparkUtils;

public class SlamApplication extends Application implements ApplicationCallback {
    public static SlamApplication mApplication;

    @Override
    public void onCreate() {
        super.onCreate();
        mApplication = this;

        SparkUtils.initSparkChainSDK(this);
        SparkUtils.initAiKitSDK(this,null);
        SparkHelper.getInstance().init(this);
    }


    private SparkService sparkService;
    private boolean isServiceConnected = false;
    private final ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            SparkService.LocalBinder binder = (SparkService.LocalBinder) service;
            sparkService = binder.getService();
            isServiceConnected = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isServiceConnected = false;
        }
    };

    @Override
    public SparkService getGlobalService() {
        return sparkService;
    }

    @Override
    public boolean isServiceConnected() {
        return isServiceConnected;
    }

    @Override
    public void startSparkService() {
        Intent intent = new Intent(this, SparkService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        startService(intent); // 确保服务启动
    }

    @Override
    public void stopSparkService() {
        if (sparkService != null) {
            stopService(new Intent(this, SparkService.class));
            sparkService = null;
        }
    }

}
